// pages/my/orderlist/orderdetail/returnrequest/returnrequest.js
import { axios } from '../../../../../utils/ajax.js'
let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //上传凭证数组
        uploadarr: [],
        //退货原因
        returnreason: [],
        reasonindex: '',
        reasonshow: false,
        userid: '',
        token: '',
        //最大退货数量
        goodsnum: '',
        //当前退货数量
        curreturnnum: 1,
        //该订单退货金额上限
        refundAmount: '',
        //退货单价
        returnPrice: '',
        maskstate: false,
        //具体说明
        additionalremarks: '',
        //是否必传图片
        mustImg: false,
        //订单号
        orderno: '',
        //退货原因源数据
        causeList: '',
        odid: '',
        //退款说明
        returnremark:'',
        //服务类型  0 仅退款(与卖家协商一致)    1 退货退款
        severtype: 1,
        /*
          货物状态  0 未收到货    1 已收到货   
          当订单状态=待成团和待发货时，默认未收到货
        */
        goodsstate: 0,
        //货物状态弹窗
        goodsstateboxshow:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        let { goodsId, specId, odid } = options
        let { userid = '', token = '' } = wx.getStorageSync('userinfo')
        this.setData({
            userid: userid,
            token: token,
            odid: odid,
            goodsId: goodsId,
            specId: specId
        })
        //获取退货原因
        axios({
            url: '/returnGoods/cause'
        }).then((res) => {
            console.log(res)
            let returnreason = this.data.returnreason
            returnreason = res.data.causeList.map(x => x.desc)
            this.setData({
                returnreason: returnreason,
                causeList: res.data.causeList
            })
        })

        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];  // 获取上一页实例   可以获取上一页的数据并改变
        console.log(prevPage.data)
      console.log(prevPage.data.data.totalPrice)
        this.setData({
          orderid: prevPage.data.orderid,
          orderno: prevPage.data.orderno,
          refundAmount: prevPage.data.data.totalPrice
        })
        // //获取退货金额
        // axios({
        //     url: '/returnGoods/refundAmount',
        //     data: {
        //         userid: this.data.userid,
        //         token: this.data.token,
        //         orderNo: this.data.orderno,
        //         odId: this.data.odid
        //     }
        // }).then((res) => {
        //     console.log(res)
        //     this.setData({
        //         refundAmount: res.data.refundAmount,
        //         goodsnum: res.data.availableNum,
        //         returnPrice: res.data.returnPrice
        //     })
        // })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //上传照片
    upload() {
        wx.chooseImage({
            count: app.globalData.maxuploadNumber, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: (res) => {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths
                console.log(tempFilePaths)
                if ( (this.data.uploadarr.length + tempFilePaths.length)<=6){
                  this.setData({
                    uploadarr: this.data.uploadarr.concat(tempFilePaths)
                  })
                }else{
                  wx.showToast({
                    title: '最多上传六张照片',
                    icon:'none'
                  })
                }
            }
        })
    },
    //提交信息
    commitreturn(e) {
        console.log(e.detail.value)
        console.log(this.data.reasonindex)
        if (this.data.reasonindex === '') {
            wx.showToast({
                title: '请选择退货原因',
                icon: "none"
            })
        }else if (this.data.mustImg && !this.data.uploadarr.length) {
            wx.showToast({
                title: '请上传照片凭证',
                icon: "none"
            })
        }else {
            console.log(this.data.mustImg)
            console.log('sj:', this.data.goodsId)
            //   return
            axios({
                url: '/returnGoods/returnRequest',
                method: "POST",
                data: {
                    userid: this.data.userid,
                    token: this.data.token,
                    orderId: this.data.orderno,
                    goodsId: this.data.goodsId,
                    specId: this.data.specId,
                    reason: this.data.causeList[this.data.reasonindex].cause,
                    amount: this.data.refundAmount,
                    quantity: this.data.curreturnnum,
                    remark: this.data.additionalremarks
                }
            }).then((res) => {
                console.log(res)  //res.data.message
                wx.showToast({
                    title: res.data.message,
                    icon: "none"
                })

                //如果传了图片
                if (this.data.uploadarr.length) {
                    console.log(this.data.userid, this.data.token, res.data.returnNo)
                    this.data.uploadarr.forEach((value, index) => {
                        wx.uploadFile({
                          url: `${app.host}/returnGoods/publishReturnGoodsImage/userid/${this.data.userid}/token/${this.data.token}/returnNo/${res.data.returnNo}`,
                            filePath: value,
                            name: 'returnrequest',
                            success: (res) => {
                                console.log(res)
                            }
                        })
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1500)
                } else {
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1500)
                }
            })
        }
    },
    //打开退货原因窗口
    showreason() {
        this.setData({
            reasonshow: true,
            maskstate: true
        })
    },
    //关闭退货原因窗口
    hidereason() {
        this.setData({
            reasonshow: false,
            maskstate: false
        })
    },
    //选择退货原因
    choosereason(e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            reasonindex: index
        })
        let mustImg = this.data.causeList[index].mustImg
        this.setData({
            mustImg: mustImg
        })
    },
    //填写用户说明
    writeremarks(e) {
        console.log(e.detail.value)
        this.setData({
            additionalremarks: e.detail.value
        })
    },
    //退款说明
    returnremark (e) {
      console.log(e.detail.value)
      this.setData({
        returnremark: e.detail.value
      })
    },
    //删除图片
  deletethis (e) {
    let index = e.currentTarget.dataset.index
    let uploadarr = this.data.uploadarr
    uploadarr.splice(index,1)
    this.setData({
      uploadarr: uploadarr
    })
  },
  //打开服务类型弹窗
  opentypebox () {
    this.setData({
      typeboxshow: true,
      maskstate: true
    })
  },
  //关闭服务类型弹窗
  hidetypebox () {
    this.setData({
      typeboxshow: false,
      maskstate: false
    })
  },
  //选择服务类型
  choosetype (e) {
    let index = e.currentTarget.dataset.index
    console.log(index)
    this.setData({
      severtype: index
    })
  },
  //打开货物状态弹窗
  openstatebox () {
    this.setData({
      goodsstateboxshow: true,
      maskstate: true
    })
  },
  //货物状态选择
  choosegoodsstate(e) {
    let index = e.currentTarget.dataset.index
    console.log(index)
    this.setData({
      goodsstate: index
    })
  },
  //关闭货物状态弹窗
  hidegoodsstatebox () {
    this.setData({
      goodsstateboxshow: false,
      maskstate: false
    })
  }
})